package nc.ac.massey.cs.plugin;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Iterator;

import nz.ac.massey.cs.domain.IConstraint;
import nz.ac.massey.cs.domain.ISudoku;
import nz.ac.massey.cs.domain.Sudoku;
import nz.ac.massey.cs.presentaion.IDrawer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.java.plugin.ObjectFactory;
import org.java.plugin.PluginManager;
import org.java.plugin.PluginManager.PluginLocation;
import org.java.plugin.registry.PluginDescriptor;
import org.java.plugin.standard.StandardPluginLocation;

public class ManagePlugins 
{
	private static PluginManager pluginManager ;
	private static void loadPlugins() 
	{
		pluginManager = ObjectFactory.newInstance().createManager(); 
		File pluginsDir = new File("..////sudokuPlugins");
		System.out.println(pluginsDir.getAbsolutePath());

		File[] plugins = pluginsDir.listFiles(new FilenameFilter() {

			public boolean accept(File dir, String name) {
				return name.toLowerCase().endsWith(".zip");
			}

		});
		try {

			PluginLocation[] locations = new PluginLocation[plugins.length];

			for (int i = 0; i < plugins.length; i++) {
				locations[i] = StandardPluginLocation.create(plugins[i]);
			}

			pluginManager.publishPlugins(locations);
			System.out.print(plugins.length);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}


	}

	public static IConstraint getconstraintplugins(String constraint)
	{
		try
		{
			loadPlugins();
			Iterator it = pluginManager.getRegistry().getPluginDescriptors().iterator();
			System.out.print(it.toString());
			while(it.hasNext())
			{				 
				PluginDescriptor p = (PluginDescriptor) it.next(); 
				if(p.getId().equals(constraint))
				{					 
					return (IConstraint) pluginManager.getPlugin(p.getId());
				}		
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return null;

	}

	public static ISudoku getsudokuplugins(String type)
	{

		if(pluginManager == null)
		{
			loadPlugins();
		}
		try
		{
			Iterator it = pluginManager.getRegistry().getPluginDescriptors().iterator();
			System.out.print(it.toString());
			while(it.hasNext())
			{
				PluginDescriptor p = (PluginDescriptor) it.next();
				if(p.getId().equals(type))
				{				
					return (ISudoku) pluginManager.getPlugin(p.getId());
				}

			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return null;
	}

	public static IDrawer getSudokuDrawingPlugins(String type)
	{
		try
		{
			Iterator it = pluginManager.getRegistry().getPluginDescriptors().iterator();
			System.out.print(it.toString());
			while(it.hasNext())
			{				 
				PluginDescriptor p = (PluginDescriptor) it.next();
				if(p.getId().equals(type))
				{
					return (IDrawer) pluginManager.getPlugin(p.getId());
				}		
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return null;
	}


}
